package org.hzero.order.api.controller.v1;

import org.hzero.core.util.Results;
import org.hzero.core.base.BaseController;
import org.hzero.order.domain.entity.UserAsyncLog;
import org.hzero.order.domain.repository.UserAsyncLogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.hzero.mybatis.helper.SecurityTokenHelper;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 用户同步日志表 管理 API
 *
 * @author qiangfei.liu@hand-china.com 2021-03-11 12:08:44
 */
@RestController("userAsyncLogController.v1")
@RequestMapping("/v1/{organizationId}/user-async-logs")
public class UserAsyncLogController extends BaseController {

    @Autowired
    private UserAsyncLogRepository userAsyncLogRepository;

    @ApiOperation(value = "用户同步日志表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<UserAsyncLog>> list(UserAsyncLog userAsyncLog, @ApiIgnore @SortDefault(value = UserAsyncLog.FIELD_ID,
            direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<UserAsyncLog> list = userAsyncLogRepository.pageAndSort(pageRequest, userAsyncLog);
        return Results.success(list);
    }

    @ApiOperation(value = "用户同步日志表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}")
    public ResponseEntity<UserAsyncLog> detail(@PathVariable Long id) {
        UserAsyncLog userAsyncLog = userAsyncLogRepository.selectByPrimaryKey(id);
        return Results.success(userAsyncLog);
    }

    @ApiOperation(value = "创建用户同步日志表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<UserAsyncLog> create(@RequestBody UserAsyncLog userAsyncLog) {
        validObject(userAsyncLog);
        userAsyncLogRepository.insertSelective(userAsyncLog);
        return Results.success(userAsyncLog);
    }

    @ApiOperation(value = "修改用户同步日志表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PutMapping
    public ResponseEntity<UserAsyncLog> update(@RequestBody UserAsyncLog userAsyncLog) {
        SecurityTokenHelper.validToken(userAsyncLog);
        userAsyncLogRepository.updateByPrimaryKeySelective(userAsyncLog);
        return Results.success(userAsyncLog);
    }

    @ApiOperation(value = "删除用户同步日志表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody UserAsyncLog userAsyncLog) {
        SecurityTokenHelper.validToken(userAsyncLog);
        userAsyncLogRepository.deleteByPrimaryKey(userAsyncLog);
        return Results.success();
    }

}
